package com.adnaan.backend.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Order;
import com.adnaan.backend.service.GoodsService;
import com.adnaan.backend.service.OrderService;
import com.adnaan.backend.service.SupplierApplicationService;
import com.adnaan.backend.utils.AuthUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestControllerMapping("/order")
@Tag(name = "订单管理", description = "订单管理")
@ApiSupport(author = "adnaan", order = 2)
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private SupplierApplicationService supplierApplicationService;

    @Resource
    private GoodsService goodsService;

    /**
     * 分页查询
     *
     * @param pageParams 分页对象
     * @return 查询结果
     */
    @PostMapping("/page")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页查询")
    public Result<Page<Order>> pageQuery(@RequestBody PageParams<Order> pageParams) {
        Order params = pageParams.getParams();
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        // 如果是供货商，只能查看自己的订单
        Long userId = StpUtil.getLoginIdAsLong();
        if (supplierApplicationService.isSupplier(userId)) {
            lqw.eq(Order::getSupplierId, userId);
        }
        lqw.eq(params.getStatus() != null, Order::getStatus, params.getStatus())
                .orderByDesc(Order::getCreateTime);
        Page<Order> page = orderService.page(pageParams.getPage(), lqw);
        page.getRecords().forEach(item -> {
            item.setGoodsName(goodsService.getById(item.getGoodsId()).getName());
        });
        return Result.success(page);
    }

    /**
     * 发货
     *
     * @param id 订单ID
     * @return 是否成功
     */
    @PostMapping("/ship/{id}")
    @ApiOperationSupport(order = 6)
    @Operation(summary = "发货")
    public Result<Boolean> ship(@PathVariable Long id) {
        // 检查是否是供货商的订单
        Order order = orderService.getById(id);
        if (order == null) {
            return Result.error("订单不存在");
        }
        Long userId = StpUtil.getLoginIdAsLong();
        if (!order.getSupplierId().equals(userId) && !AuthUtils.isAdmin()) {
            return Result.error("您无权发货此订单");
        }
        return Result.success(orderService.updateStatus(id, 2));
    }
} 